package com.cskaoyan.junit;

        import com.cskaoyan.Account;
        import com.cskaoyan.utils.DruidUtils;
        import org.apache.commons.dbutils.QueryRunner;
        import org.apache.commons.dbutils.handlers.BeanHandler;
        import org.apache.commons.dbutils.handlers.BeanListHandler;
        import org.junit.After;
        import org.junit.AfterClass;
        import org.junit.BeforeClass;
        import org.junit.Test;

        import java.sql.Connection;
        import java.sql.SQLException;
        import java.util.List;

public class AccountTest {

    static QueryRunner queryRunnerWithoutDatasource;
    static QueryRunner queryRunnerWithDatasource;

    static Connection connection;

    @BeforeClass
    public static void init(){

        queryRunnerWithoutDatasource = new QueryRunner();
        queryRunnerWithDatasource = new QueryRunner(DruidUtils.getDataSource());

        connection = DruidUtils.getConnection();

    }

    @After
    public void after(){

//        try {
//            connection.commit();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

    }


    @AfterClass
    public static void destory(){

        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 测试查询所有的
    @Test
    public void testSelectAll() throws SQLException {

        List<Account> accounts = queryRunnerWithoutDatasource.query(connection,"select * from account",new BeanListHandler<Account>(Account.class));

        System.out.println(accounts);

    }


    // 根据id查询
    @Test
    public void testSelectAccountById() throws SQLException {

        Account account = queryRunnerWithDatasource.query("select * from account where id = ?", new BeanHandler<>(Account.class), 2);

        System.out.println(account);

    }

    @Test
    public void testSelect(){

        Connection connection = DruidUtils.getConnection();

    }
}
